$(function () {
    //购物车列表
    // let token=localStorage.getItem('token') || sessionStorage.getItem('token')
    $('tbody').html(`<tr>
              <th class="desc">项目</th>
              <th class="unit-price">单价</th>
              <th class="num">数量</th>
              <th class="total-price">总价</th>
              <th style="width:40px">操作</th>
            </tr>`)
    $.get('/api/carts/index.jsp',{token},res=>{
      // console.log(res);
      let totalPrice=0;
      $.each(res.data,(i,item)=>{
        // totalPrice+=item.goods_number*item.shop_price
        totalPrice=((totalPrice*100)+(((item.goods_number*100)*(item.shop_price*100))/10000)*100)/100
        let html=`<tr data-id="${item.goods_id}">
              <td class="title">
                <a href="./waimai-detail.html?store_id=${item.store_id}" data-reactid="12">${item.store_name} ${item.goods_name}</a>
                <p>${item.goods_attr}</p>
              </td>
              <td>¥${item.shop_price}</td>
              <td data-reactid="16">
                <div class="input-number">
                  <button class="minus-btn" type="button">-</button>
                  <input type="text" class="input" value="${item.goods_number}">
                  <button class="plus-btn" type="button">+</button>
                </div>
              </td>
              <td data-reactid="21">¥${((item.goods_number*100)*(item.shop_price*100))/10000}</td>
              <td><a href="javascript:;">删除</a></td>
            </tr>`
            $('tbody').append(html)
      })

      $('.total-price b').html(totalPrice)
    },'json')


    //购物车删除
    $('body').on('click','a',function(){
      let goods_id=$(this).parents('tr').attr('data-id')
      $.get('/api/carts/delete.jsp',{token,goods_id},res=>{
        console.log(res);
        if(res.meta.state===200){
          $(this).parents('tr').remove();
          location.reload()
        }else{
          
          cocoMessage.error(res.meta.msg);
        }
      },'json')
    })
}) 
